package com.zhongkun.datahome.service.processor.dto;

import cn.hutool.core.convert.impl.StringConverter;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.zhongkun.datahome.common.annotation.Excel;
import com.zhongkun.datahome.service.constant.MaterialChannelEnum;
import com.zhongkun.datahome.service.constant.MaterialTypeEnum;
import com.zhongkun.datahome.service.constant.SkuStatusEnum;
import com.zhongkun.datahome.service.domain.HouseMaterial;
import com.zhongkun.datahome.service.export.converter.QuantityConverter;
import com.zhongkun.datahome.service.export.converter.StringDiagonalConverter;
import com.zhongkun.datahome.service.export.converter.UrlListConverter;
import com.zhongkun.datahome.service.req.cmd.sku.SkuCmd;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.net.URL;
import java.text.MessageFormat;
import java.util.List;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/24 10:37
 * @Desc
 */
@Data
@HeadRowHeight(25)
public class HouseMaterialDto {
    // 记录所在行号
    @ExcelIgnore
    private int rowIndex;

    /** 客户信息 */
    @NotNull(message = "【客户姓名】不能为空")
    @Length(min = 2, max = 32, message = "【客户姓名】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 1, value = "客户姓名")
    //@ColumnWidth(16)
    @ApiModelProperty("客户姓名")
    @Excel(name = "客户姓名")
    private String customerName;

    @NotNull(message = "【客户手机号】不能为空")
    @Length(min = 6, max = 11, message = "【客户手机号】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 2, value = "客户手机号")
    //@ColumnWidth(18)
    @ApiModelProperty("客户手机号")
    @Excel(name = "客户手机号")
    private String mobile;

    /** 房产信息 */
    @NotNull(message = "【楼栋号】不能为空")
    @Length(min = 1, max = 32, message = "【楼栋号】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 3, value = "楼栋号")
    //@ColumnWidth(14)
    @ApiModelProperty("楼栋号")
    @Excel(name = "楼栋号")
    private String buildingNum;

    @NotNull(message = "【房号】不能为空")
    @Length(min = 2, max = 32, message = "【房号】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 4, value = "房号")
    //@ColumnWidth(12)
    @ApiModelProperty("房号")
    @Excel(name = "房号")
    private String roomNum;

    @NotNull(message = "【户型名称】不能为空")
    @Length(min = 2, max = 32, message = "【户型名称】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 5, value = "户型名称")
    //@ColumnWidth(16)
    @ApiModelProperty("户型名称")
    @Excel(name = "户型名称")
    private String houseName;

    @NotNull(message = "【风格】不能为空")
    @Length(min = 2, max = 32, message = "【风格】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 6, value = "风格")
    //@ColumnWidth(12)
    @ApiModelProperty("风格")
    @Excel(name = "风格")
    private String decoratorStyle;

    @NotNull(message = "【签约状态】不能为空")
    @Length(min = 2, max = 32, message = "【签约状态】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 7, value = "签约状态")
    //@ColumnWidth(16)
    @ApiModelProperty("签约状态")
    @Excel(name = "签约状态")
    private String signStatus;


    /** 材料清单信息 */
    @NotNull(message = "【所属空间】不能为空")
    @Length(min = 2, max = 32, message = "【所属空间】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 8, value = "所属空间")
    //@ColumnWidth(16)
    @ApiModelProperty("所属空间")
    @Excel(name = "所属空间")
    private String room;

    @NotNull(message = "【类目名称】不能为空")
    @Length(min = 2, max = 32, message = "【类目名称】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 9, value = "类目名称")
    //@ColumnWidth(16)
    @ApiModelProperty("类目名称")
    @Excel(name = "类目名称")
    private String category;

    @ExcelProperty(index = 10, value = "数量", converter = QuantityConverter.class)
    //@ColumnWidth(12)
    @ApiModelProperty("数量")
    @Excel(name = "数量")
    private BigDecimal quantity;

    @NotNull(message = "【单位】不能为空")
    @Length(min = 1, max = 32, message = "【单位】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 11, value = "单位")
    //@ColumnWidth(12)
    @ApiModelProperty("单位")
    @Excel(name = "单位")
    private String unit;

    @NotNull(message = "【商品名称】不能为空")
    @Length(min = 2, max = 64, message = "【商品名称】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 12, value = "商品名称")
    //@ColumnWidth(16)
    @ApiModelProperty("商品名称")
    @Excel(name = "商品名称")
    private String skuName;

    //@NotNull(message = "【品牌】不能为空")
    @Length(min = 0, max = 32, message = "【品牌】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 13, value = "品牌", converter = StringDiagonalConverter.class)
    //@ColumnWidth(12)
    @ApiModelProperty("品牌")
    @Excel(name = "品牌")
    private String brandName;

    // @NotNull(message = "【型号】不能为空")
    @Length(min = 0, max = 128, message = "【型号】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 14, value = "型号", converter = StringDiagonalConverter.class)
    //@ColumnWidth(12)
    @ApiModelProperty("型号")
    @Excel(name = "型号")
    private String specification;

    @NotNull(message = "【渠道】不能为空")
    @Length(min = 2, max = 32, message = "【渠道】长度应该在{min}到{max}之间")
    @ExcelProperty(index = 15, value = "渠道")
    //@ColumnWidth(12)
    @ApiModelProperty("渠道")
    @Excel(name = "渠道")
    private String channel;

    @Length(max = 512, message = "【备注】长度不能超过{max}")
    @ExcelProperty(index = 16, value = "备注", converter = StringDiagonalConverter.class)
    //@ColumnWidth(12)
    @ApiModelProperty("备注")
    @Excel(name = "备注")
    private String remark;

    @ExcelProperty(index = 17, value = "图片", converter = UrlListConverter.class)
    @ApiModelProperty("图片")
    private List<String> skuPics;

    @ExcelIgnore
    private List<String> urls;

    @ExcelIgnore
    private Long skuId;


    public String getHouseKey() {
        return MessageFormat.format("{0}-{1}", this.getBuildingNum(), this.getRoomNum());
    }

    public String getSkuKey(){
        return MessageFormat.format("{0}-{1}-{2}-{3}-{4}", this.getSkuName(), this.getCategory(), this.getUnit(), this.getBrandName(), this.getSpecification());
    }

    public SkuCmd convertSku() {
        SkuCmd sku = new SkuCmd();
        sku.setBrandName(this.getBrandName());
        sku.setCategory(this.getCategory());
        sku.setSkuName(this.getSkuName());
        sku.setUnit(this.getUnit());
        sku.setSpecification(this.getSpecification());
//        sku.setRemark(this.getRemark());
        sku.setStatus(SkuStatusEnum.ON_SHELF.getStatus());
        return sku;
    }

    public HouseMaterial convertMaterial(Long houseId, Long skuId) {
        HouseMaterial houseMaterial = new HouseMaterial();

        houseMaterial.setHouseId(houseId);
        houseMaterial.setMaterialType(MaterialTypeEnum.COMMON_SKU.getType());
        houseMaterial.setMaterialId(skuId);
        houseMaterial.setQuantity(this.getQuantity());
        houseMaterial.setRoom(this.getRoom());
        houseMaterial.setChannel(MaterialChannelEnum.getByName(this.getChannel()));
        houseMaterial.setRemark(this.getRemark());
        return houseMaterial;
    }
}
